package com.xhwl.data.controller;


import com.google.common.collect.Lists;
import com.xhwl.common.dto.cent.sdata.PermissionDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.Project;
import com.xhwl.common.query.cent.sdata.ProjectQuery;
import com.xhwl.common.utils.ChkUtil;
import com.xhwl.data.service.IPermissionQuickEntryService;
import com.xhwl.data.service.IProjectService;
import com.xhwl.data.util.AccountUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 首页快捷入口
 */
@RestController
public class PermissionQuickEntryController {

    @Autowired
    IPermissionQuickEntryService iPermissionQuickEntryService;

    @Autowired
    IProjectService projectService;

    @Autowired
    PermissionController permissionController;

    /**
     * 快捷入口列表
     */
    @GetMapping(value = "/quick-entry/list/{projectId}")
    public ResultJson<List<PermissionDTO>> listQuickEntry(Account operateAccount, @PathVariable Integer projectId) {
        AccountUtil.checkLogin(operateAccount);
        return ResultJson.success(iPermissionQuickEntryService.list(projectId, operateAccount.getId()));
    }

    /**
     * 快捷入口保存
     */
    @PostMapping(value = "/quick-entry/save/{projectId}")
    public ResultJson saveQuickEntry(Account operateAccount, @PathVariable Integer projectId, @Validated @RequestBody LinkedList<Integer> list) {
        if (ChkUtil.isNull(list)) {
            return ResultJson.fail("请选择菜单项");
        }
        AccountUtil.checkLogin(operateAccount);
        iPermissionQuickEntryService.save(projectId, operateAccount.getId(), list);
        return ResultJson.success();
    }

    /**
     * 获取末级菜单信息并且支持搜索和平铺
     */
    @GetMapping(value = "/quick-entry/menu/{projectId}")
    public ResultJson<List<PermissionDTO>> menuTile(Account operateAccount,
                                                    @RequestHeader(value = "appId") String appId,
                                                    @PathVariable Integer projectId,
                                                    @RequestParam(value = "keyword", required = false) String keyword,
                                                    @RequestParam(value = "excludeIds", required = false) String excludeIds) {
        AccountUtil.checkLogin(operateAccount);
        ProjectQuery projectQuery = new ProjectQuery();
        projectQuery.withIdList(Collections.singletonList(projectId));
        Project project = projectService.getOne(projectQuery, null);
        if (project == null) {
            return ResultJson.fail("项目不存在");
        }
        ResultJson resultJson = permissionController.menuList(operateAccount, project.getIndustryId(), appId, projectId);
        List<PermissionDTO> results = (List<PermissionDTO>) resultJson.getResult();
        if (ChkUtil.isNull(results)) {
            return resultJson;
        }
        List<Integer> excludeIdArr = Lists.newArrayList();
        if (ChkUtil.isNotNull(excludeIds)) {
            int[] arr = Arrays.stream(excludeIds.split(",")).mapToInt(Integer::parseInt).toArray();
            excludeIdArr = Arrays.stream(arr).boxed().collect(Collectors.toList());
        }
        return ResultJson.success(iPermissionQuickEntryService.menuTile(results, keyword, excludeIdArr));
    }

    /**
     * 获取三方登录菜单
     */
    @GetMapping(value = "/quick-entry/three-menu/{projectId}")
    public ResultJson<List<PermissionDTO>> threeMenuTile(Account operateAccount,
                                                         @RequestHeader(value = "appId") String appId,
                                                         @PathVariable Integer projectId) {
        AccountUtil.checkLogin(operateAccount);
        ProjectQuery projectQuery = new ProjectQuery();
        projectQuery.withIdList(Collections.singletonList(projectId));
        Project project = projectService.getOne(projectQuery, null);
        if (project == null) {
            return ResultJson.fail("项目不存在");
        }
        ResultJson resultJson = permissionController.menuList(operateAccount, project.getIndustryId(), appId, projectId);
        List<PermissionDTO> results = (List<PermissionDTO>) resultJson.getResult();
        if (ChkUtil.isNull(results)) {
            return resultJson;
        }
        return ResultJson.success(iPermissionQuickEntryService.threeMenuTile(results));
    }
}


